459. 重复的子字符串
为保证权益,题目请参考 459. 重复的子字符串(From LeetCode).
解决方案1
CPP
C++
//
// Created by lenovo on 2020-08-24.
//
#include <iostream>
using namespace std;
class Solution {
public:
bool repeatedSubstringPattern(string s) {
int n = s.length();
for (int i = 1; i < n; i++) {
if (n % i == 0) {
string t = s.substr(0, i);
if(isit(s, t)){
return true;
}
}
}
return false;
}
/**
* 判断t是不是s的循环子串
*
* @param s
* @param t
* @return
*/
bool isit(const string & s, const string & t){
int sn = s.length();
int tn = t.length();
for(int i = 0; i < sn; i += tn){
if(s.substr(i, tn) != t) {
return false;
}
}
return true;
}
};
int main() {
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Python
python
"""
LeetCode 459
https://leetcode-cn.com/problems/repeated-substring-pattern/
https://leetcode-cn.com/problems/repeated-substring-pattern/
https://leetcode-cn.com/problems/repeated-substring-pattern/
"""
class Solution:
"""
静态类
"""
def repeatedSubstringPattern(self, s: str) -> bool:
"""
题目要求
:param s:
:return:
"""
for i in range(1, len(s), 1):
if len(s) % i == 0:
t = s[0:i]
if self.isit(s, t):
return True
return False
def isit(self, s: str, t: str) -> bool:
"""
判断t是否是S的循环子串
:param s: 大串
:param t: 小串
:return: bool
"""
for i in range(0, len(s), len(t)):
if s[i:i + len(t)] != t:
return False
return True
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42